<?php
class PortfoliosController extends PortfoliosAppController {

	var $name = 'Portfolios';
	var $uses = array('Portfolios.Portfolio', 'Portfolios.Upload','Portfolios.Category');
	var $helpers = array('Html', 'Form', 'Text', 'Time','Extend.Textile','Extend.GershImage', 'Extend.Lightbox');
	var $components = array('RequestHandler', 'Session');
	var $paginate = array(
		'limit' => 16,
		'order' => array('Portfolio.date' => 'desc'),
		'conditions' => array('Portfolio.published' => 1)
	);
	
	function beforeFilter() {
		 	parent::beforeFilter();
			$this->pageTitle = $this->name;
			$access = $this->Session->read('unlocked');
			//debug($this->paginate);
			if($access == 1) {
				$this->paginate['conditions'] = array();
			}
		}
		
	function unlock() {
		$this->Session->write('unlocked', true);
		$this->Session->setFlash(__('You now have access to all portfolio items!', true));
		$this->redirect(array('action'=>'index'));
	}
	
	function lock() {
		$this->Session->delete('unlocked');
		$this->Session->setFlash(__('You now have access has been removed', true));
		$this->redirect(array('action'=>'index'));
	}

	function index($cat = null) {
		
		//$this->Portfolio->recursive = 1;
		if($cat == null) {
			$portfolios = $this->paginate();
			$active = 'all';
		} else {
			$active = $cat;
			$params = array(
				'contain' => false,
				'fields' => array('DISTINCT (Portfolio.id) as id'),
				'conditions' => $this->paginate['conditions'],
				'joins' => array()
			);
			$joins1 = array(
				'table' => $this->Portfolio->tablePrefix.'portfolios_categories_portfolios',
				'alias' => 'CategoriesPortfolio',
				'type' => 'inner',
				'foreignKey' => false,
				'conditions'=> array('CategoriesPortfolio.portfolio_id = Portfolio.id')
			);
			$joins2 = array(
				'table' => $this->Portfolio->tablePrefix.'portfolios_categories',
				'alias' => 'Category',
				'type' => 'inner',
				'foreignKey' => false,
				'conditions'=> array(
				'Category.id = CategoriesPortfolio.category_id',
				'Category.slug' => $cat
				)
			);
			array_push($params['joins'], $joins1);
			array_push($params['joins'], $joins2);
			
			// query
			$bars = array();
			foreach ($this->Portfolio->find('all', $params) as $row) {
				array_push($bars, $row['Portfolio']['id']);
			}	

			// do basic pagination; id must appear in list of ids from previous query
			$portfolios = $this->paginate(null, array('Portfolio.id' => $bars));


		}
		
		$categories = $this->Portfolio->Category->find('list', array('fields' => array('Category.slug', 'Category.title')) );
		$this->set(compact('categories', 'active'));
		$this->set('portfolios', $portfolios);
	}
	function recent($num = 10) {
		//$this->pageTitle = $num . ' ' . __('recent posts', false);
		$params = array(
			'conditions' => $this->paginate['conditions'],
			'recursive' => 1, //int
			'order' => $this->paginate['order'], 
			'limit' => $num
		);
		$items = $this->Portfolio->find('all', $params);
		if(isset($this->params['requested'])) { 
			return $items; 
		}
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Portfolio.', true));
			$this->redirect(array('action'=>'index'));
		}
		if(is_numeric($id)) {
			$portfolio = $this->Portfolio->read(null, $id);
		} else {
			$portfolio = $this->Portfolio->findBySlug($id);
		}
		$access = $this->Session->read('unlocked');
		if($access =! 1) {
		if($portfolio['Portfolio']['published'] != 1) {
			$this->Session->setFlash(__('Invalid Portfolio.', true));
			$this->redirect(array('action'=>'index'));
		}
	}
		$this->pageTitle = $portfolio['Portfolio']['title'] . " / " . $this->name;
		//debug($portfolio['Portfolio']['id']);
		$neighbors = $this->Portfolio->find('neighbors', array('field' => 'id', 'value' => $portfolio['Portfolio']['id'], 'conditions' => $this->paginate['conditions']));
		$this->set('neighbors', $neighbors);
		$this->set('portfolio', $portfolio);
		
			// Update view count
			$this->Portfolio->id = $portfolio['Portfolio']['id'];
			$current_views = $portfolio['Portfolio']['views']  = $portfolio['Portfolio']['views'] + 1;
			$this->Portfolio->saveField('views', $current_views);
	}

	function admin_index() {
		$this->Portfolio->recursive = 1;
		$this->paginate['conditions'] = null;
		$this->paginate['order'] =array('Portfolio.created' => 'desc');
		$this->set('portfolios', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Portfolio.', true));
			$this->redirect('/admin/portfolios/');
		}
		$this->set('portfolio', $this->Portfolio->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Portfolio->create();
			if ($this->Portfolio->save($this->data)) {
				$this->Session->setFlash(__('The Portfolio has been saved', true));
				$this->redirect('/admin/portfolios/');
			} else {
				$this->Session->setFlash(__('The Portfolio could not be saved. Please, try again.', true));
			}
		}
		$categories = $this->Portfolio->Category->find('list');
		$this->set(compact('categories'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Portfolio', true));
			$this->redirect('/admin/portfolios/');
		}
		if (!empty($this->data)) {
			if ($this->Portfolio->save($this->data)) {
				$this->Session->setFlash(__('The Portfolio has been saved', true));
				$this->redirect('/admin/portfolios/');
			} else {
				$this->Session->setFlash(__('The Portfolio could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Portfolio->read(null, $id);
		}
		$categories = $this->Portfolio->Category->find('list');
		$this->set(compact('categories'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Portfolio', true));
			$this->redirect('/admin/portfolios/');
		}
		if ($this->Portfolio->del($id)) {
			$this->Session->setFlash(__('Portfolio deleted', true));
			$this->redirect('/admin/portfolios/');
		}
	}

}
?>